/* rqoqty.p   - DETERMINE REQUISITION LINE OPEN QUANTITY                      */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/*V8:ConvertMode=NoConvert                                                    */
/* Revision: 8.6    LAST MODIFIED BY: 04/22/97  By: B. Gates          *J1Q2*  */
/* REVISION: 8.6E     LAST MODIFIED : 05/08/00   BY: *L0XC* Sandeep Rao       */
/* REVISION: 9.1      LAST MODIFIED : 08/12/00   BY: *N0KP* myb               */
/* $Revision: 1.6 $  BY: Stefan Sepanaho       DATE: 08/30/00  ECO: *N0QK*     */
/* REVISION: 9.1      LAST MODIFIED : 09/11/00   BY: *M0SH* Ashwini G.        */

         {mfdeclre.i}


         define input parameter p_first_call as logical no-undo.
         define input parameter p_req_site like pod_site no-undo.
         define input parameter p_req_nbr like rqpo_req_nbr no-undo.
         define input parameter p_req_line like rqpo_req_line no-undo.
         define output parameter p_qty_open like rqd_req_qty no-undo.
         define output parameter p_req_um like rqd_um no-undo.

         define variable old_db   as character      no-undo.
         define variable err-flag as integer        no-undo.
/*M0SH*/ define variable l_um     like um_conv      no-undo.
/*M0SH*/ define variable l_rum    like um_conv      no-undo.
/*M0SH*/ define variable l_qty    like pod_qty_ord  no-undo.
/*M0SH*/ define variable l_retqty like pod_qty_rtnd no-undo.


         if p_first_call then do:
            /*FIRST TIME IN, WE SWITCH DB'S IF WE HAVE TO THEN
            CALL OURSELVES AGAIN, WHICH WILL EXECUTE THE ELSE BLOCK*/
            find si_mstr where si_site = p_req_site no-lock.

            if si_db <> global_db then do:
               old_db = global_db.
               {gprun.i ""gpalias3.p"" "(si_db, output err-flag)"}
            end.

            {gprun.i ""rqoqty.p""
            "(input no, input p_req_site, input p_req_nbr, input p_req_line,
            output p_qty_open, output p_req_um)"}

            /*SWITCH BACK TO CENTRAL PO DB IF WE HAVE TO*/
            if old_db <> global_db then do:
               {gprun.i ""gpalias3.p"" "(old_db, output err-flag)"}
            end.
         end.
         else do:
            find rqd_det
               where rqd_nbr = p_req_nbr and rqd_line = p_req_line
               no-lock no-error.

            if not available rqd_det then do:
               p_qty_open = ?.
               leave.
            end.

            p_qty_open = rqd_req_qty.

            for each rqpo_ref
               fields(rqpo_po_line rqpo_po_nbr rqpo_req_line rqpo_req_nbr)
               no-lock
               where rqpo_req_nbr  = rqd_nbr
               and   rqpo_req_line = rqd_line:

               for each pod_det
/*M0SH**          fields(pod_nbr pod_line pod_qty_ord pod_qty_rtnd) */
/*M0SH*/          fields(pod_line pod_nbr pod_part pod_qty_ord
/*M0SH*/                 pod_qty_rtnd pod_rum pod_type pod_um)
                  where pod_nbr      = rqpo_po_nbr
                    and pod_line     = rqpo_po_line no-lock:

/*M0SH**/      /* BEGIN ADD */

                  if pod_type <> "M"
                  then do:

                     /* PERFORM UM CONVERSION IF PO UM AND REQUISITION UM  */
                     /* ARE DIFFERENT                                      */
                     if pod_um <> rqd_um
                     then do:
                        {gprun.i ""gpumcnv.p""
                           "(input  pod_um,
                             input  rqd_um,
                             input  pod_part,
                             output l_um)"}
                        if l_um = ?
                           then l_um = 1.
                        l_qty = pod_qty_ord * l_um.
                     end. /* IF pod_um <> rqd_um */

                     else /* IF pod_um = rqd_um */
                        l_qty = pod_qty_ord.

                     if pod_qty_rtnd <> 0
                     then do:
                        if pod_rum <> rqd_um
                        then do:
                           {gprun.i ""gpumcnv.p""
                              "(input  pod_rum,
                                input  rqd_um,
                                input  pod_part,
                                output l_rum)"}
                           if l_rum = ?
                              then l_rum = 1.
                           l_retqty = pod_qty_rtnd * l_rum.
                        end. /* IF pod_rum <> rqd_um */

                        else /* IF pod_rum = rqd_um */
                           l_retqty = pod_qty_rtnd.

                     end. /* IF pod_qty_rtnd <> 0 */

                     p_qty_open = p_qty_open - (l_qty - l_retqty).

                  end. /* IF pod_type <> "M" */

                  else
/*M0SH**/      /* END ADD */
                     p_qty_open = p_qty_open - (pod_qty_ord - pod_qty_rtnd).

/*M0SH*/          assign
/*M0SH*/             l_qty    = 0
/*M0SH*/             l_retqty = 0.

               end. /* FOR EACH POD_DET */

            end. /* FOR EACH RQPO_REF */

            p_req_um = rqd_um.
         end.
